/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetlif13.view;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.CanvasGameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;
import projetlif13.model.Game;
import projetlif13.model.puissance4.HPlayerP4;
import projetlif13.model.puissance4.IAPlayerP4;
import projetlif13.model.puissance4.P4Game;
import projetlif13.model.puissance43D.HPlayerP43D;
import projetlif13.model.puissance43D.IAPlayerP43D;
import projetlif13.model.puissance43D.P43DGame;
import projetlif13.model.tictactoe.HPlayerTic;
import projetlif13.model.tictactoe.IAPlayerTic;
import projetlif13.model.tictactoe.TicGame;
import projetlif13.model.tournament.TournamentV2;
import projetlif13.scene.GameScene;
import projetlif13.tools.PreferencesBundler;

/**
 *
 * @author yohan
 */
public final class GameView extends javax.swing.JFrame {

    public static String p1name = "Joueur 1";
    public static String p2name = "Joueur 2";
    private static Game game;
    public static GameScene gameScene;
    private CanvasGameContainer container;

    public GameView() {
    }

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    /**
     * Creates new form GameView
     */
    public GameView(Game gb) throws SlickException {
        game = gb;
        initComponents();

        gameScene = new GameScene();
        container = new CanvasGameContainer(gameScene);
        container.getContainer().setAlwaysRender(true);
        container.getContainer().setShowFPS(false);
        container.getContainer().setClearEachFrame(true);
        container.getContainer().setVSync(true);
        container.setSize(500, 500);
        container.setBounds(0, 0, 500, 500);
        jPanel1.add(container);


        try {
            container.start();
        } catch (SlickException e1) {
            Log.error(e1);
        }
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                container.getContainer().exit();
                container.dispose();
                dispose();
                System.exit(0);
            }
        });


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Morpion et Puissance4 par T.J & Y.B");
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        setResizable(false);

        jButton1.setText("Rejouer");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setPreferredSize(new java.awt.Dimension(380, 380));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jButton2.setText("Choisir noms joueurs humains");
        jButton2.setToolTipText("Seront actualisés à la partie suivante. Valable pour session en cours uniquement.");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jMenuBar1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jMenuBar1.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        jMenuBar1.setMinimumSize(null);

        jMenu1.setText("Jeu");

        jMenu3.setText("Nouveau jeu");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Morpion");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("Puissance 4");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem8.setText("Puissance 4 3D");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem8);

        jMenu1.add(jMenu3);

        jMenuItem1.setText("Nouveau tournois");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);
        jMenu1.add(jSeparator4);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        jMenuItem2.setText("Option");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        jMenuItem7.setText("Modifier l'apparence");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem7);
        jMenu1.add(jSeparator3);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem6.setText("Quitter");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("?");

        jMenuItem5.setText("A propos");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(0, 46, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void stopPreviousGame() {
        if (game != null) {
            game.stop();
        }
    }

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        //lancement p4 nouvelle partie
        stopPreviousGame();
        GameScene.gameBoard = GameScene.TYPE_P4;
        PreferencesBundler.putValue("game.board", "" + GameScene.gameBoard);
        switch (GameScene.gameType) {
            case Game.TYPE_EVE:
                game = new P4Game(new IAPlayerP4(), new IAPlayerP4());
                break;
            case Game.TYPE_PVP:
                game = new P4Game(new HPlayerP4("*J* " + p1name), new HPlayerP4("*J* " + p2name));
                break;
            case Game.TYPE_PVE:
                game = new P4Game(new HPlayerP4("*J* " + p1name), new IAPlayerP4());
                break;
        }
        gameScene.reinit();

        GameScene.uitambourine.play();
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        stopPreviousGame();
        GameScene.gameBoard = GameScene.TYPE_TIC;
        PreferencesBundler.putValue("game.board", "" + GameScene.gameBoard);
        switch (GameScene.gameType) {
            case Game.TYPE_EVE:
                game = new TicGame(new IAPlayerTic(), new IAPlayerTic());
                break;
            case Game.TYPE_PVP:
                game = new TicGame(new HPlayerTic("*J* " + p1name), new HPlayerTic("*J* " + p2name));
                break;
            case Game.TYPE_PVE:
                game = new TicGame(new HPlayerTic("*J* " + p1name), new IAPlayerTic());
                break;
        }

        gameScene.reinit();

        GameScene.uitambourine.play();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
        //exit
        container.getContainer().exit();
        container.dispose();
        dispose();
        System.exit(0);
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        this.setEnabled(false);
        SettingsView sv = new SettingsView(this);
        sv.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        // TODO add your handling code here:
        //launch SkinView
        this.setEnabled(false);
        SkinView sv = new SkinView(this);
        sv.setVisible(true);
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (game instanceof P4Game) {
            jMenuItem4ActionPerformed(null);
            PreferencesBundler.putValue("game.board", "" + GameScene.TYPE_P4);
        } else if (game instanceof TicGame) {
            jMenuItem3ActionPerformed(null);
            PreferencesBundler.putValue("game.board", "" + GameScene.TYPE_TIC);

        } else if (game instanceof P43DGame) {
            jMenuItem8ActionPerformed(null);
            PreferencesBundler.putValue("game.board", "" + GameScene.TYPE_P43D);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        Apropos a = new Apropos();
        a.setVisible(true);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        stopPreviousGame();
        GameScene.gameBoard = GameScene.TYPE_P43D;
        PreferencesBundler.putValue("game.board", "" + GameScene.gameBoard);
        switch (GameScene.gameType) {
            case Game.TYPE_EVE:
                game = new P43DGame(new IAPlayerP43D(), new IAPlayerP43D());
                break;
            case Game.TYPE_PVP:
                game = new P43DGame(new HPlayerP43D("*J* " + p1name), new HPlayerP43D("*J* " + p2name));
                break;
            case Game.TYPE_PVE:
                game = new P43DGame(new HPlayerP43D("*J* " + p1name), new IAPlayerP43D());
                break;
        }
        gameScene.reinit();
        GameScene.uitambourine.play();

    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        new ChoiceNames(this).setVisible(true);
        setEnabled(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed

        new Thread(new Runnable() {

            @Override
            public void run() {
                TournamentV2 tn = new TournamentV2(GameScene.gameBoard);
                GameScene.uitambourine.play();
                for (int i = 0; i < SettingsView.nbplayers; i++) {
                    switch (GameScene.gameBoard) {
                        case GameScene.TYPE_P4:
                            tn.addPlayer(new IAPlayerP4());
                            break;
                        case GameScene.TYPE_P43D:
                            tn.addPlayer(new IAPlayerP43D());
                            break;
                        case GameScene.TYPE_TIC:
                            tn.addPlayer(new IAPlayerTic());
                            break;
                    }

                }
                tn.init();
                gameScene.setWinner(tn.playTournament());
            }
        }).start();


    }//GEN-LAST:event_jMenuItem1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    // End of variables declaration//GEN-END:variables

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    Game g = null;
                    /*
                     * Savoir quel dernier type,plateau de jeu
                     */
                    PreferencesBundler.load();
                    int type = Integer.parseInt(PreferencesBundler.getValue("game.type", "" + Game.TYPE_PVE));//pvp etc
                    int board = Integer.parseInt(PreferencesBundler.getValue("game.board", "" + GameScene.TYPE_TIC));//pvp etc
                    switch (type) {

                        case Game.TYPE_EVE:
                            switch (board) {
                                case GameScene.TYPE_P4:
                                    g = new P4Game(new IAPlayerP4(), new IAPlayerP4());
                                    break;
                                case GameScene.TYPE_TIC:
                                    g = new TicGame(new IAPlayerTic(), new IAPlayerTic());
                                    break;
                                case GameScene.TYPE_P43D:
                                    g = new P43DGame(new IAPlayerP43D(), new IAPlayerP43D());
                                    break;
                            }

                            break;
                        case Game.TYPE_PVP:
                            switch (board) {
                                case GameScene.TYPE_P4:
                                    g = new P4Game(new HPlayerP4("*J* " + p1name), new HPlayerP4("*J* " + p2name));
                                    break;
                                case GameScene.TYPE_TIC:
                                    g = new TicGame(new HPlayerTic("*J* " + p1name), new HPlayerTic("*J* " + p2name));
                                    break;
                                case GameScene.TYPE_P43D:
                                    g = new P43DGame(new HPlayerP43D("*J* " + p1name), new HPlayerP43D("*J* " + p2name));
                                    break;

                            }

                            break;
                        case Game.TYPE_PVE:
                            switch (board) {
                                case GameScene.TYPE_P4:
                                    g = new P4Game(new HPlayerP4("*J* " + p1name), new IAPlayerP4());
                                    break;
                                case GameScene.TYPE_TIC:
                                    g = new TicGame(new HPlayerTic("*J* " + p1name), new IAPlayerTic());
                                    break;
                                case GameScene.TYPE_P43D:
                                    g = new P43DGame(new HPlayerP43D("*J* " + p1name), new IAPlayerP43D());
                                    break;

                            }

                            break;

                    }

                    GameView gv = new GameView(g);
                    gv.setVisible(true);
                    GameScene.gv = gv;

                } catch (SlickException ex) {
                    Logger.getLogger(GameView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}
